import {
  getTokenByCode
} from '/api/user'

import {
  getMchInfo
} from '/api/amap'

import Event from '/utils/Event';

const util = require('/utils/util.js')

//app.js
App({
  event: new Event(),
  onLaunch: function (options) {
    console.log("onLaunch")

    var _this = this;
    _this.screenSize(); //获取可视窗口的尺寸

    wx.getSystemInfo({
      success: function (res) {
        _this.globalData.rpxR = 750 / res.windowWidth;
        _this.globalData.windowWidth = res.windowWidth * _this.globalData.rpxR;
        _this.globalData.windowHeight = res.windowHeight * _this.globalData.rpxR;
        _this.globalData.statusBarHeight = res.statusBarHeight * _this.globalData.rpxR;
      }
    })
    _this.globalData.uuid = util.getUUID()
    // 检查是否登录
    wx.login({
      success: res => {
        console.log(res);
        // 发送 res.code 到后台换取 openId 并获取令牌
        getTokenByCode({
          mchId: _this.globalData.mchId,
          auth_code: res.code,
          uuid: _this.globalData.uuid,
          pluginId: 'weixinMiniAuthPlugin'
        }).then(
          res => {
            console.log(res,'获取token')
            if (res.type == 'success') {
              const {
                userInfo,
                userId,
                token
              } = res.data
              _this.globalData.uuid = userId
              _this.globalData.token = token
              _this.globalData.userInfo = userInfo
              // console.log(_this.globalData)
              // console.log('1')
            }
            if (_this.APP_TYPE > 0) {
              getMchInfo({ xuid: _this.globalData.xuid }).then(res => {
                if (res.data != null && res.data.sysMch != null) {
                  _this.globalData.mchId = res.data.sysMch.id
                  _this.globalData.mchName = res.data.sysMch.name
                  _this.globalData.location = res.data.locked;
                  if (res.data.shop != null) {
                    _this.globalData.shopId = res.data.shop.id
                    _this.globalData.shopName = res.data.shop.name
                  }
                }
                if (_this.userInfoReadyCallback) {
                  // console.log('查看是否有调用')
                  _this.globalData.loaded = true;
                  _this.userInfoReadyCallback(res)
                }
              }).catch(error => {
                console.log(error)
              })
              console.log('2')
            } else {
              if (_this.userInfoReadyCallback) {
                _this.globalData.location = true;
                console.log('查看是否有调用2')
                _this.globalData.loaded = true;
                _this.userInfoReadyCallback(res)
              }
              // console.log('3')
            }
          }
        ).catch(error => {
          console.log(error)
          // console.log('4')
        })
      },
      fail: err => {
        if (_this.APP_TYPE > 0) {
          if (_this.userInfoReadyCallback) {
            console.log('查看是否有调用3')
            _this.globalData.loaded = true;
            _this.userInfoReadyCallback(res)
          }
          console.log('5')
        } else {
          if (_this.userInfoReadyCallback) {
            _this.globalData.loaded = true;
            _this.globalData.location = true;
            console.log('查看是否有调用4')
            _this.userInfoReadyCallback(res)
          }
          console.log('6')
        }
      }
    })


  },
  // APP_BASE_API: 'https://boot.rzico.com:9090',
  APP_BASE_API: 'https://api.rzico.com:8443',
  // APP_BASE_API: 'http://192.168.2.232:8080',
    // APP_BASE_API: 'https://boot.hulu.fj.cn:8080',
  // APP_BASE_API: 'https://api.rzico.com:8443',
  //     APP_BASE_API: 'https://dev.hulu.fj.cn:8443',
  //APP_BASE_API: 'https://app.zenofresh.com:8443',
  //APP_BASE_API: 'https://api.h0594.net:8443',
  // APP_BASE_API:'http://192.168.2.232:8080',
  // 0 单商户版 1.服务商版
  APP_TYPE: 0,
  // 0 会员卡、金币、福利卡 1.钱包、空桶压金、积分 2.钱包、会员卡、积分 3.金币、提货券、积分,4.积分，钱包，库存
  VERSION: 4,
  // 1:标准版,2:水站版,3:餐饮版,4.商超版,5.景区版
  SCOPE_TYPE: 1,

  // 0不开启（礼品卡）
  GIFT_TYPE:0,  

  ISV_ID: '10236',
  SHARE_TYPE:0, //0:普通   1：为流量
  globalData: {

    // mapKey: 'RFIBZ-7KMH3-HE532-3ZHCX-VTU6Z-SOBJW',  //智密购
    // mchId: '10302',        //智密购  wx09c9990f783e5130
    // mchName:'智密购众联',
    // mapKey: 'AC4BZ-DTSCQ-JFX5B-GG4WQ-2NDC6-GYFMZ',  //七甲路

    // mchId: '10202',        //七甲路
    // mapKey: '75TBZ-CUV6K-SEKJJ-AYQMN-KN4QT-PUBT7',  //葫芦派
    // mchId: '10200', //葫芦派  wxeae1a099a54f95ca

    // mapKey: '75TBZ-CUV6K-SEKJJ-AYQMN-KN4QT-PUBT7',  //测试机
    // mchId: '2619a672e53811e7b983201a068c6482',  //测试机

    // mapKey: 'B53BZ-KEYKX-AXW4B-7VUQD-CGOJJ-NSFFI',  //长富鲜生  wx4f66466bdb2e5f0d
    // mchId: '10206',  //长富鲜生
    // mchName:'长富鲜生',

    //  mchName:'厦门桶装水配送',
    //  mapKey: 'IXVBZ-FLXWS-6WIOQ-6JP7J-3CLGF-3UFPX',  //	厦门桶装水配送 wx50bd85301c0d0287
    //  mchId: '10213',  //厦门桶装水配送

    // mapKey: 'VAMBZ-IBT3U-VAUVG-B7XQW-HCDPK-L3B5M', //珍农在线 wxc25c2dd586daf2fa
    // mchId: '10200', //珍农在线
    // mchName: '珍农在线',

    // mchName:'渴了鸭',
    // mapKey: '7GQBZ-MVPK5-F6KII-QA3B3-DMWES-EUBHX',  //	渴了鸭 wxeef5397833680dd7
    // mchId: '10210',  //渴了鸭

    // mchName:'水捷达',
    // mapKey: 'MBSBZ-AEAC3-NYL3U-YZIGH-GQPDO-XABJX',  //	水捷达 wx028cae9453f60bc8
    // mchId: '10216',  //水捷达

     mchName:'芸订水',
     mapKey: 'QBABZ-43ZHO-R3FWZ-SJCA7-GH3YQ-QZBVN',  //	芸订水 wx98ca23561599628f
     mchId: '10200',  //渴了鸭

    //  mchName:'阿宾哥送水',
    //  mapKey: '73OBZ-DAPWX-47Y4O-ZGZ7J-ZZCCQ-ETB7O',  //	阿宾哥送水 wxf8951f3680a6b09c
    //  mchId: '10215',  //阿宾哥送水

    //  mchName:'万仓',
    //  mapKey: 'OM6BZ-HAVCS-7R7OJ-6OHU6-NCF2E-T2F6B',  //	万仓 wxd6d3442bb2bdc627
    //  mchId: '10218',  //万仓

    //  mchName:'金翔云众创优选',
    //  mapKey: 'CMQBZ-ZTS3D-5L54Z-PLERB-JD67J-Q5BOI',  //	金翔云众创优选 wxf5c0e6d87ea90b1b
    //  mchId: '10225',  //金翔云众创优选

    // mchName:'柚鱼康美',
    // mapKey: 'YF5BZ-BTQ3W-2T6RU-OLZGX-JQQ5J-5ZFHP',  //	柚鱼康美 wx459fe85528b0cac5
    // mchId: '10221',  //柚鱼康美

    // mchName:'鑫忠廉',
    // mapKey: 'GE2BZ-NL2WU-JGBVT-224UT-WDXS2-ZYBQG',  //	鑫忠廉 wxd175f54ac60251b7
    // mchId: '10224',  //鑫忠廉

    // mapKey: 'C5OBZ-YFERI-BZTG4-5GK7A-RNYJO-FVBZN', //533潮品 wxe77067965f09c5f8
    // mchId: '10200', //533潮品
    // mchName: '533潮品',


      //  mchName:'水动圈',
      //  mapKey: 'ITEBZ-3NVWJ-CFRFL-KZDYI-ZC2B5-YTF3C',  //	水动圈 wx649b7e0405bbbfa9
      //  mchId: '10234',  //水动圈

    // mchName:'订水网',
    // mapKey: 'T44BZ-IZUCR-OYOW2-W6XDI-DBC4H-5QBWR',  //	订水网 wxe2439ebc4c4fe6a2
    // mchId: '10219',  //订水网

    // mchName:'水湃湃',
    // mapKey: 'JZTBZ-6QWEQ-NS65R-GXELS-273LO-2IBAM',  //	水湃湃 wxeeaf25d628e882ba
    // mchId: '10233',  //水湃湃

    // mchName:'娇蓉美业',
    // mapKey: 'MRPBZ-Y7PK6-CRHSO-MNQ7C-XK6EF-WUB6G',  //	娇蓉美业 wx21668e161ecfb387
    // mchId: '10236',  //中旅旅业

    //  mchName:'易点桶装水供应链',
    //  mapKey: 'X4LBZ-CYVLJ-TPKFN-FINU6-K5RT2-QWFAW',  //	易点桶装水供应链wx1790b7df6a1485ce
    //  mchId: '10229',  //易点桶装水供应链

    location: false,
    uuid: null,
    token: null,
    userInfo: {id:''},
    xuid: null,
    shopId: null,
    shopName:null,
    loaded: false
  },
  //获取可视窗口宽度
  screenSize: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        //可视窗口宽度
        var ww = res.windowWidth;
        //可视窗口高度
        var hh = res.windowHeight;
        that.globalData.ww = ww;
        that.globalData.hh = hh;
      }
    })
  },

})
